package com.aaa.controller;

import com.aaa.entity.BlackLists;
import com.aaa.entity.R;
import com.aaa.mapper.BlackListsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 黑白名单管理
 */
@RestController
@RequestMapping("/blackLists")
public class BlackListsController {

    @Resource
    private BlackListsMapper blackListsMapper;

   /**
    * 分页查询
    */
    @RequestMapping("/selectPage")
    public Object selectPage(BlackLists blackLists,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        List<BlackLists> list = blackListsMapper.selectList(blackLists);
        PageInfo<BlackLists> pageInfo = new PageInfo<>(list);
        Map<String, Object> data = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        data.put("code", 0);
        data.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        data.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        data.put("data", pageInfo.getList());
        return data;
    }

    /**
     * 移入黑名单
     */
    @PostMapping("/toBlack")
    public R toBlack(@RequestBody BlackLists blackLists) {
        int i = blackListsMapper.updateToBlack(blackLists);
        return i > 0 ? R.ok() : R.error();
    }



    /**
     * 移出黑名单
     */
    @PostMapping("/toWhite")
    public R toWhite(@RequestBody BlackLists blackLists) {
        int i = blackListsMapper.updateToWhite(blackLists);
        return i > 0 ? R.ok() : R.error();
    }

    /**
     * 新增黑名单
     */
    @PostMapping("/add")
    public R add(@RequestBody BlackLists blackLists) {

        int i = blackListsMapper.addBlackLists(blackLists);
        return i > 0 ? R.ok() : R.error();
    }


}
